package com.lefevre.cms.web.action;

import com.lefevre.cms.bean.setting.SystemSetting;
import com.lefevre.cms.service.setting.SettingService;
import org.springframework.mobile.device.Device;
import org.springframework.mobile.device.DeviceResolver;
import org.springframework.mobile.device.LiteDeviceResolver;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 访问来源设备
 *
 * @author Lefevre
 */
@Component("accessSourceDeviceManage")
public class AccessDeviceUtil {

    private final DeviceResolver deviceResolver = new LiteDeviceResolver();

    @Resource
    SettingService settingService;

    /**
     * 访问设备
     *
     * @return pc:电脑 wap:移动设备
     */
    public String accessDevices(HttpServletRequest request) {
        SystemSetting systemSetting = settingService.findSystemSetting_cache();
        if (systemSetting != null && systemSetting.getSupportAccessDevice() != null) {
            if (systemSetting.getSupportAccessDevice().equals(2)) {
                //电脑端
                return "pc";
            } else if (systemSetting.getSupportAccessDevice().equals(3)) {
                //移动设备
                return "wap";
            }
        }
        Device device = deviceResolver.resolveDevice(request);
        if (device.isNormal()) {
            //电脑
            return "pc";
        } else {
            //移动设备
            return "wap";
        }
    }

}
